package org.suren.littlebird.sync;

import org.eclipse.core.internal.resources.File;
import org.eclipse.jface.preference.IPreferenceStore;
import org.suren.littlebird.Activator;
import org.suren.littlebird.preferences.PreferenceConstants;
import org.suren.littlebird.ssh.ScpUtils;
import org.suren.littlebird.views.InstallBundleDialog;

/**
 * @author suren
 * @date 2015-4-3
 *
 * http://surenpi.com
 */
public class FileSync
{
	private String sshHost;
	private int sshPort;
	private String sshUser;
	private String sshPasswd;
	private String dstFile;

	/**
	 * @param dialog
	 * @param file
	 * @return
	 */
	public boolean bundleHandler(InstallBundleDialog dialog, File file)
	{
		return bundleHandler(dialog, file.getLocation().makeAbsolute().toFile());
	}

	public boolean bundleHandler(InstallBundleDialog dialog, java.io.File file)
	{
		String absPath = file.getAbsolutePath();
		String name = file.getName();
		if(dialog.open(absPath) != InstallBundleDialog.OK)
		{
			return false;
		}

		IPreferenceStore store = Activator.getDefault().getPreferenceStore();

		sshHost = store.getString(PreferenceConstants.P_OSGI_SERVER);
		sshPort = store.getInt(PreferenceConstants.P_SSH_PORT);
		sshUser = store.getString(PreferenceConstants.P_SSH_USER);
		sshPasswd = store.getString(PreferenceConstants.P_SSH_PASSWD);

		String bundleDir = dialog.getBundleDir();
		dstFile = bundleDir + "/" + name;

		ScpUtils scpUtils = new ScpUtils(sshHost, sshPort, sshUser, sshPasswd);
		return scpUtils.scp(dialog.getBundleFile(), dstFile);
	}

	public String getDstFile()
	{
		return dstFile;
	}

}
